package pw.hais.braintwists.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.squareup.okhttp.Response;

import org.json.JSONObject;

import pw.hais.braintwists.R;
import pw.hais.braintwists.app.BaseActivity;
import pw.hais.braintwists.dao.LeanCloudDao;
import pw.hais.braintwists.model.User;
import pw.hais.http.base.OnHttpListener;
import pw.hais.utils.L;
import pw.hais.utils.SPUtils;

/**
 * 注册页面
 * Created by Hais1992 on 2015/12/11.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private EditText edit_username, edit_name, edit_password;
    private TextView text_author;
    private Button btn_reg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        text_author = (TextView) findViewById(R.id.text_author);
        edit_username = (EditText) findViewById(R.id.edit_username);
        edit_name = (EditText) findViewById(R.id.edit_name);
        edit_password = (EditText) findViewById(R.id.edit_password);
        btn_reg = (Button) findViewById(R.id.btn_reg);
        btn_reg.setOnClickListener(this);
        text_author.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_reg:
                if (verificationInfo()) {
                    doRegUser();
                }
                break;
            case R.id.text_author:
                startActivity(new Intent(context,AuthorActivity.class));
                break;
        }
    }

    private String username;
    private String password;
    private String name;

    //验证信息
    private boolean verificationInfo() {
        username = edit_username.getText() == null ? "" : edit_username.getText().toString().trim();
        password = edit_password.getText() == null ? "" : edit_password.getText().toString().trim();
        name = edit_name.getText() == null ? "" : edit_name.getText().toString().trim();

        if ("".equals(username) || "".equals(password) || "".equals(name)) {
            L.showShort("用户名、密码不能为空！");
        } else {
            return true;
        }

        return false;
    }

    //开始注册
    private void doRegUser() {
        loadDialogShow("注册中...");
        LeanCloudDao.regUser(username, password, name, new OnHttpListener<User>() {
            @Override
            public void onSuccess(Response response, User data) {
                loadDialogDismiss();
                if(data.error !=null){
                    L.showShort("注册失败,"+data.error+"~~");
                }else{
                    L.showShort(data.name+"，恭喜您注册成功！");
                    app.user = data;
                    SPUtils.saveObject("User",app.user);
                    setResult(222);
                    finish();
                }
            }
        });


    }
}
